// 获取菜单父子关系
const getLevel = (menu) => {
  const newMenu = []
  menu.filter(j => j.level === 0).map(m => {
    newMenu.push(m)
  })
  return newMenu
}

// 给所有项添加子数组
const addChild = (list) => {
  list.map(j => {
    if (!j['children']) {
      j['children'] = []
      j['childIds'] = []
    }
  })
  return list
}

// 获取json tree结构
const getJson = (menu) => {
  addChild(menu).map(x => {
    addChild(menu).map(y => {
      if (y.pid === x.id && !x['childIds'].includes(y.id)) {
        x['childIds'].push(y.id)
        x['children'].push(y)
      }
    })
  })
  return getLevel(menu)
}

export default getJson
